﻿// var authority = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理
if ((authority[16]) === 0) {
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}
var dummyData = [];
var postData;
$.ajax({
    url: '/user/staff',
    type: 'post',
    data: {
        "requestType": "allList"
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
for (let i = 0; i < postData.length; i++) {
    dummyData.push([
        postData[i].staff_id,
        postData[i].user_name,
        postData[i].user_account
    ]);
}
var title;
var methodCount;
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
if (authority[16] === 1) {
    title = ["编号", "姓名", "手机号", "主管", "成员"];
    methodCount = 3;
}
var table = document.getElementById("table0");
var tbody = table.children[1];
var pages = document.getElementById("pages0");
//初始化表格
for (let i = 0; i < title.length; i++) {
    let t = document.createElement("th");
    t.innerHTML = title[i];
    table.children[0].children[0].appendChild(t);
}
for (let i = 0; i < dummyData.length; i++) {
    let inner = "";
    for (let j = 0; j < methodCount; j++) {
        inner += "<td>" + dummyData[i][j] + "</td>";
    }
    inner += "<td><input type=\"checkbox\" onclick=\"CheckLeader(this)\"></td>";
    inner += "<td><input type=\"checkbox\" onclick=\"Check(this)\"></td>";
    var tr = document.createElement("tr");
    tr.innerHTML = inner;
    tbody.appendChild(tr);
}

var leaderIndex;
var memberIndex = [];
function CheckLeader(method) {
    if (method.checked) {
        if (leaderIndex !== undefined) {
            tbody.children[leaderIndex].children[3].children[0].checked = false;
        }
        leaderIndex = $(method.parentNode.parentNode).index();
        tbody.children[leaderIndex].children[4].children[0].checked = false;
        if (memberIndex.indexOf($(method.parentNode.parentNode).index()) > -1) {
            memberIndex.splice(memberIndex.indexOf($(method.parentNode.parentNode).index()), 1);
        }
    } else {
        leaderIndex = undefined;
    }
    console.log(leaderIndex + memberIndex);
}

function Check(element) {
    tbody.children[$(element.parentNode.parentNode).index()].children[3].children[0].checked = false;
    if (leaderIndex === $(element.parentNode.parentNode).index()) {
        leaderIndex = undefined;
    }
    if (element.checked) {
        memberIndex.push($(element.parentNode.parentNode).index());
    } else {
        memberIndex.splice(memberIndex.indexOf($(element.parentNode.parentNode).index()), 1);
    }
    console.log(leaderIndex + memberIndex);
}
function OnCommit() {
    if (leaderIndex === undefined) {
        alert("你还没有选择主管！");
        return;
    }
    let leaderID = tbody.children[leaderIndex].children[0].innerHTML;
    let memberID = [];
    for (let i = 0; i < memberIndex.length; i++) {
        memberID[i] = tbody.children[memberIndex[i]].children[0].innerHTML;
    }
    memberID.push(leaderID);
    $.ajax({
        url: '/user/department',
        type: 'post',
        data: {
            "part_name": document.getElementById("groupName").value,
            "part_m": leaderID,
            "requestType": "insert"
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    console.log(postData);
    $.ajax({
        url: '/user/staff',
        type: 'post',
        data: {
            "staff_part": postData.insertId,
            "staff_team": null,
            "staff_id": memberID,
            "requestType": "update"
        },
        traditional: true,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                alert("提交成功");
                history.back();
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}